import request from '@/router/axios';
import Mock from 'mockjs';
import { Random } from 'mockjs'
//商品名称 和图片
// 创建自定义 Mock 函数
Random.extend({
    // 自定义函数名: function 函数  
    fruit: function() {    
      const arr = [
          '樱の色烟酰胺润肤补水五件套提拉控油保湿滋润面部套盒护肤品套装',
          '肤姿美蜂蜜补水保湿面膜控油滋润嫩滑毛孔细致护肤化妆品面膜',
          '束渍油污净 厨房油烟机清洗剂去重油污清洁剂除油剂贴牌定制',
          '束渍香水洗衣液 女士洗衣露内衣贴身衣物持久留香不伤衣物',
          '束渍薰衣草香水洗衣液2kg厂家OEM 贴牌代加工定制',
          '洗洁精1.2kg大桶厨房餐具洗涤剂果蔬净工厂直销洗碗液',
          '束渍2kg香水洗衣液瓶装贴牌代工OEM厂家直销正品赠品持久留香',
          '高端洗洁精柠檬香型餐具清洁剂洗碗液洗涤剂贴牌厂家直销oem',
          '【正品】束渍椰油洗衣液2kg  厂家直销 现货 OEM代工贴牌',
          '【正品包邮】香水洗衣液2kg 持久留香厂家直销oem贴牌代加工'
      ]
      return this.pick(arr)  
  }
})
Random.extend({
  // 自定义函数名: function 函数  
  imgFn: function() {    
    const arr = [
        'https://cbu01.alicdn.com/img/ibank/2020/582/018/18258810285_1442181519.400x400.jpg',
        'https://cbu01.alicdn.com/img/ibank/2018/419/635/8985536914_1442181519.400x400.jpg',
        'https://cbu01.alicdn.com/img/ibank/2020/181/066/18940660181_220139942.400x400.jpg',
        'https://cbu01.alicdn.com/img/ibank/2020/111/468/23561864111_220139942.400x400.jpg',
        'https://cbu01.alicdn.com/img/ibank/O1CN013pVl4d1OtUuVKbVNe_!!6000000001763-0-cib.400x400.jpg',
        'https://cbu01.alicdn.com/img/ibank/2020/114/085/22581580411_220139942.400x400.jpg',
        'https://cbu01.alicdn.com/img/ibank/2020/150/057/23561750051_220139942.400x400.jpg',
        'https://cbu01.alicdn.com/img/ibank/2020/375/529/18872925573_220139942.400x400.jpg',
        'https://cbu01.alicdn.com/img/ibank/2020/618/407/17934704816_220139942.400x400.jpg',
        'https://cbu01.alicdn.com/img/ibank/2020/019/086/22122680910_220139942.400x400.jpg'
    ]
    return this.pick(arr)  
}
})

let index=''; //支付方式
let num='';    // 支付状态
Random.extend({
    // 自定义函数名: function 函数  
    telFn: function() {    
      const phonePrefix = ['132', '135', '189','156','138','181','186','136','176']
      const index = Math.floor(Math.random() * phonePrefix.length)
      var phone = phonePrefix[index] + Mock.mock(/\d{8}/);
      return this.pick(phone)
    },
    payMethodFn: function() {    
        index = Math.ceil(Math.random() * 3)
        return this.pick(index)
    },
    orderStatusFn: function() { 
        if(index==1){
            num = Math.ceil(Math.random() * 2)
        }else{
            num = Math.round(Math.random()*2+3)
        }
        return this.pick(num)
    },
    disFn: function() {
        let arr = [
            "顺丰快递",
            "百世快递",
            "圆通快递",
            "中通快递",
            "韵达快递",
        ]
        if(num>3){
            return this.pick(arr)
        }else{
            return "暂无"
        }
    },
    singleNumFn:function() {
        if(num>3){
            return Mock.mock(/\d{12}/)
        }else{
            return "暂无"
        }
    },
    timeFn:function(){
        let arr = [
            '2022-04-15 15:39:34',
            '2022-04-18 18:35:26',
            '2022-04-18 12:10:56',
            '2022-04-16 19:34:31',
            '2022-04-09 18:45:26',
            '2022-04-10 14:05:06',
            '2022-04-11 10:13:08',
            '2022-04-12 09:03:30',
            '2022-04-13 17:50:43',
            '2022-04-14 11:15:42',
        ]
        return this.pick(arr)
    },
    detailAddFn:function(){
        let arr = [
            '东山街道',
            '清流街道街道',
            '凤凰湖街道',
            '长城街道街道',
            '鼎山街道',
            '泰山街道',
            '淳化街道',
        ]
        return this.pick(arr)
    }
})

let list = Mock.mock({
    "tableList|15-30":[
        {
            id:/\d{7}/, // 订单编号
            time:'@timeFn()',//提交时间
            account:'@word',//用户账号
            allPrice:0, //订单金额
            payMethod:"@payMethodFn()",//支付方式 1 未支付  2微信  3支付宝
            "source|1-2":10, //来源 1app  2 pc端
            "ordertype|1-2":10, // 订单类型  1 正常订单  2 秒杀订单
            orderStatus:"@orderStatusFn()",//1 已取消  2待付款  3 待发货  4 已发货 5已完成
            logistics:"@disFn()",//配送方式
            singleNum:"@singleNumFn()", //物流单号
            orderNote:'', // 订单备注
            "reward|100-200":1,// 可得成长值 和积分
            userChlidren:[
                {
                    username:'@cname', //用户名
                    tel:"@telFn()",   //手机号
                    zip:'@zip', //邮政编码
                    address:'@county(true)',//收货地址
                    deliAddr:'@county(true)', //发货地址
                    detailAdd:'@detailAddFn()' //详细地址
                }
            ],
            "goodsList|1-6":[
                {
                    id:'@zip',     //商品货号
                    goodsname:'@fruit()',  //商品名称
                    picUrl:'@imgFn()', 
                    "price|50-300.2": 1,   // 当前价格
                    "num|1-3":5 //数量
                },
            ],
            preChildren:{
                "freight|0-20":1,//运费
                "active|10-50":1, //活动优惠
                "coupons|0-15":1, //优惠券
                "integral|0-10":1, //积分
                "discount|0-10":1 // 折扣金额
            },

            
        }
    ]
})

//获取数据列表
export const orderList = (data) =>request({
    url:'/orderList',
    method:'get',
    data
})

Mock.mock(/\/orderList/,config=>{
    let {pagenum,pagesize} = JSON.parse(config.body);
    let _list = list.tableList;
    let total = _list.length;
    if(pagenum && pagesize){
        let user = _list.filter((item,index)=>index<pagesize*pagenum && index>=pagesize*(pagenum-1));
        return {
            data:{
                total,
                user,pagenum,pagesize
            },
            meta:{
                msg:'获取成功',
                status:200
            }
            
        }
    }else{
        return {
            meta:{
                msg:'获取失败',
                status:400
            }
        }
    }
   
})

//根据id获取单个数据
export const orderobj = (data) =>request({
    url:'/orderobj',
    method:'get',
    data
})

Mock.mock(/\/orderobj/,config=>{
    let {id} = JSON.parse(config.body);
    let _list = list.tableList;
    if(id){
        let user = _list.filter((item,index)=>item.id==id);
        return {
            data:{
                user
            },
            meta:{
                msg:'获取成功',
                status:200
            }
            
        }
    }else{
        return {
            meta:{
                msg:'获取失败',
                status:400
            }
        }
    }
   
})

//发货接口
export const deliOrder = (data) =>request({
    url:'/deliOrder',
    method:'put',
    data
})

Mock.mock(/\/deliOrder/,config=>{
    let {id,logistics,singleNum} = JSON.parse(config.body);
    let _list = list.tableList;
    if(id && logistics && singleNum){
        for(let item of _list){
           if(item.id==id){
               item.orderStatus = 4;
               item.logistics = logistics==1?"顺丰快递":logistics==2?"百世快递":logistics==3?"圆通快递":logistics==4?"中通快递":"韵达快递";
               item.singleNum = singleNum
           }
        }
        let user = _list.filter((item,index)=>item.id==id);
        return {
            data:{
                user
            },
            meta:{
                msg:'获取成功',
                status:200
            }
            
        }
    }else{
        return {
            meta:{
                msg:'获取失败',
                status:400
            }
        }
    }
   
})

//修改收货人信息
export const editorderUser = (data) =>request({
    url:'/editorderUser',
    mothed:'put',
    data
})
Mock.mock(/\/editorderUser/,config=>{
    let datas = JSON.parse(config.body);
    let {id} = datas;
    let _list = list.tableList;
    if(id){
        //username,tel,zip,address,detailAdd
        for(let item of _list){
            if(item.id==id){
                let items = item.userChlidren[0]
                items.username = datas.username?datas.username:items.username;
                items.tel = datas.tel?datas.tel:items.tel;
                items.zip = datas.zip?datas.zip:items.zip;
                items.address = datas.address?datas.address:items.address;
                items.detailAdd = datas.detailAdd?datas.detailAdd:items.detailAdd;
            }
        }
        return {
            meta:{
                msg:'修改成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'参数错误',
                status:400
            }
        }
    }
   
})

//修改运费
export const editMoney = (data) =>request({
    url:'/editMoney',
    mothed:'put',
    data
})
Mock.mock(/\/editMoney/,config=>{
    let datas = JSON.parse(config.body);
    let {id,freight} = datas;
    let _list = list.tableList;
    if(id && freight>=0){
        for(let item of _list){
            if(item.id==id){
                item.preChildren.freight = freight;
            }
        }
        return {
            meta:{
                msg:'修改成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'参数错误',
                status:400
            }
        }
    }
   
})

//添加备注
export const editnode = (data) =>request({
    url:'/editnode',
    mothed:'put',
    data
})
Mock.mock(/\/editnode/,config=>{
    let datas = JSON.parse(config.body);
    let {id} = datas;
    let _list = list.tableList;
    if(id){
        for(let item of _list){
            if(item.id==id){
                item.orderNote = datas.orderNote?datas.orderNote:item.orderNote;
            }
        }
        return {
            meta:{
                msg:'备注成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'参数错误',
                status:400
            }
        }
    }
   
})

//取消订单
export const cancelOrder = (data) =>request({
    url:'/cancelOrder',
    mothed:'put',
    data
})
Mock.mock(/\/cancelOrder/,config=>{
    let datas = JSON.parse(config.body);
    let {id} = datas;
    let _list = list.tableList;
    if(id){
        for(let item of _list){
            if(item.id==id){
                item.orderStatus = 1
            }
        }
        return {
            meta:{
                msg:'备注成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'参数错误',
                status:400
            }
        }
    }
   
})

//删除订单
export const detOrder = (data) =>request({
    url:'/detOrder',
    mothed:'delete',
    data
})
Mock.mock(/\/detOrder/,config=>{
    let {id} = JSON.parse(config.body);
    let _list = list.tableList;
    if(id){
        _list.splice(_list.findIndex(item=>item.id==id),1)
        return {
            meta:{
                msg:'删除成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'删除失败',
                status:400
            }
        }
    }
    
})

//查询 根据收货人 手机号 订单类型 订单来源  订单状态 
// id,username,orderStatus,ordertype,source
export const queryOrder = (data) =>request({
    url:'/queryOrder',
    mothed:'get',
    data
})
Mock.mock(/\/queryOrder/,config=>{
    let datas = JSON.parse(config.body);
    let _list = list.tableList;
    let arr = [];
    if(datas.id || datas.username || datas.orderStatus || datas.ordertype || datas.source){
        if(datas.id){
            arr = _list.filter((item,index)=>JSON.stringify(item.id).indexOf(datas.id)>=0);
        }
        if(datas.username){
            if(isNaN(datas.username*1)){
                arr = (arr.length==0?_list:arr).filter((item,index)=>item.userChlidren[0].username.indexOf(datas.username)>=0);
            }else{
                arr = (arr.length==0?_list:arr).filter((item,index)=>item.userChlidren[0].tel.indexOf(datas.username)>=0);
            }
            
        }
        if(datas.orderStatus){
            arr = (arr.length==0?_list:arr).filter((item,index)=>item.orderStatus==datas.orderStatus);
        }
        if(datas.ordertype){
            arr = (arr.length==0?_list:arr).filter((item,index)=>item.ordertype==datas.ordertype);
        }
        if(datas.source){
            arr = (arr.length==0?_list:arr).filter((item,index)=>item.source==datas.source);
        }
        let total = arr.length;
        let pagesize = arr.length
        return {
            data:{
                arr,total,pagesize
            },
            meta:{
                msg:'查询成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'参数错误',
                status:400
            }
        }
    }
})